home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / gdb / inflow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  11.9 KB  |  492 lines

  1. /* Low level interface to ptrace, for GDB when running under Unix.
  2.    Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25. #include "command.h"
  26. #include "signals.h"
  27. #include "terminal.h"
  28. #include "target.h"
  29.  
  30. #ifdef USG
  31. #include <sys/types.h>
  32. #endif
  33.  
  34. /* Some USG-esque systems (some of which are BSD-esque enough so that USG
  35.    is not defined) want this header, and it won't do any harm.  */
  36. #include <fcntl.h>
  37.  
  38. #include <sys/param.h>
  39. #include <sys/dir.h>
  40. #include <signal.h>
  41.  
  42. extern struct target_ops child_ops;
  43.  
  44. /* Nonzero if we are debugging an attached outside process
  45.    rather than an inferior.  */
  46.  
  47. int attach_flag;
  48.  
  49.  
  50. /* Record terminal status separately for debugger and inferior.  */
  51.  
  52. static TERMINAL sg_inferior;
  53. static TERMINAL sg_ours;
  54.  
  55. static int tflags_inferior;
  56. static int tflags_ours;
  57.  
  58. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  59. static struct tchars tc_inferior;
  60. static struct tchars tc_ours;
  61. #endif
  62.  
  63. #ifdef TIOCGLTC
  64. static struct ltchars ltc_inferior;
  65. static struct ltchars ltc_ours;
  66. #endif
  67.  
  68. #ifdef TIOCLGET
  69. static int lmode_inferior;
  70. static int lmode_ours;
  71. #endif
  72.  
  73. #ifdef TIOCGPGRP
  74. static int pgrp_inferior;
  75. static int pgrp_ours;
  76. #else
  77. static void (*sigint_ours) ();
  78. static void (*sigquit_ours) ();
  79. #endif /* TIOCGPGRP */
  80.  
  81. /* Copy of inferior_io_terminal when inferior was last started.  */
  82. static char *inferior_thisrun_terminal;
  83.  
  84. static void terminal_ours_1 ();
  85.  
  86. /* Nonzero if our terminal settings are in effect.
  87.    Zero if the inferior's settings are in effect.  */
  88. static int terminal_is_ours;
  89.  
  90. /* Initialize the terminal settings we record for the inferior,
  91.    before we actually run the inferior.  */
  92.  
  93. void
  94. terminal_init_inferior ()
  95. {
  96.   sg_inferior = sg_ours;
  97.   tflags_inferior = tflags_ours;
  98.  
  99. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  100.   tc_inferior = tc_ours;
  101. #endif
  102.  
  103. #ifdef TIOCGLTC
  104.   ltc_inferior = ltc_ours;
  105. #endif
  106.  
  107. #ifdef TIOCLGET
  108.   lmode_inferior = lmode_ours;
  109. #endif
  110.  
  111. #ifdef TIOCGPGRP
  112.   pgrp_inferior = inferior_pid;
  113. #endif /* TIOCGPGRP */
  114.  
  115.   terminal_is_ours = 1;
  116. }
  117.  
  118. /* Put the inferior's terminal settings into effect.
  119.    This is preparation for starting or resuming the inferior.  */
  120.  
  121. void
  122. terminal_inferior ()
  123. {
  124.   if (terminal_is_ours && inferior_thisrun_terminal == 0)
  125.     {
  126.       fcntl (0, F_SETFL, tflags_inferior);
  127.       fcntl (0, F_SETFL, tflags_inferior);
  128.       ioctl (0, TIOCSETN, &sg_inferior);
  129.  
  130. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  131.       ioctl (0, TIOCSETC, &tc_inferior);
  132. #endif
  133. #ifdef TIOCGLTC
  134.       ioctl (0, TIOCSLTC, <c_inferior);
  135. #endif
  136. #ifdef TIOCLGET
  137.       ioctl (0, TIOCLSET, &lmode_inferior);
  138. #endif
  139.  
  140. #ifdef TIOCGPGRP
  141.       ioctl (0, TIOCSPGRP, &pgrp_inferior);
  142. #else
  143.       sigint_ours = (void (*) ()) signal (SIGINT, SIG_IGN);
  144.       sigquit_ours = (void (*) ()) signal (SIGQUIT, SIG_IGN);
  145. #endif /* TIOCGPGRP */
  146.     }
  147.   terminal_is_ours = 0;
  148. }
  149.  
  150. /* Put some of our terminal settings into effect,
  151.    enough to get proper results from our output,
  152.    but do not change into or out of RAW mode
  153.    so that no input is discarded.
  154.  
  155.    After doing this, either terminal_ours or terminal_inferior
  156.    should be called to get back to a normal state of affairs.  */
  157.  
  158. void
  159. terminal_ours_for_output ()
  160. {
  161.   terminal_ours_1 (1);
  162. }
  163.  
  164. /* Put our terminal settings into effect.
  165.    First record the inferior's terminal settings
  166.    so they can be restored properly later.  */
  167.  
  168. void
  169. terminal_ours ()
  170. {
  171.   terminal_ours_1 (0);
  172. }
  173.  
  174. static void
  175. terminal_ours_1 (output_only)
  176.      int output_only;
  177. {
  178. #ifdef TIOCGPGRP
  179.   /* Ignore this signal since it will happen when we try to set the pgrp.  */
  180.   void (*osigttou) ();
  181. #endif /* TIOCGPGRP */
  182.  
  183.   /* The check for inferior_thisrun_terminal had been commented out
  184.      when the call to ioctl (TIOCNOTTY) was commented out.
  185.      Checking inferior_thisrun_terminal is necessary so that
  186.      if GDB is running in the background, it won't block trying
  187.      to do the ioctl()'s below.  */
  188.   if (inferior_thisrun_terminal != 0)
  189.     return;
  190.  
  191.   if (!terminal_is_ours)
  192.     {
  193.       terminal_is_ours = 1;
  194.  
  195. #ifdef TIOCGPGRP
  196.       osigttou = (void (*) ()) signal (SIGTTOU, SIG_IGN);
  197.  
  198.       ioctl (0, TIOCGPGRP, &pgrp_inferior);
  199.       ioctl (0, TIOCSPGRP, &pgrp_ours);
  200.  
  201.       signal (SIGTTOU, osigttou);
  202. #else
  203.       signal (SIGINT, sigint_ours);
  204.       signal (SIGQUIT, sigquit_ours);
  205. #endif /* TIOCGPGRP */
  206.  
  207.       tflags_inferior = fcntl (0, F_GETFL, 0);
  208.       ioctl (0, TIOCGETP, &sg_inferior);
  209.  
  210. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  211.       ioctl (0, TIOCGETC, &tc_inferior);
  212. #endif
  213. #ifdef TIOCGLTC
  214.       ioctl (0, TIOCGLTC, <c_inferior);
  215. #endif
  216. #ifdef TIOCLGET
  217.       ioctl (0, TIOCLGET, &lmode_inferior);
  218. #endif
  219.     }
  220.  
  221. #ifdef HAVE_TERMIO
  222.   sg_ours.c_lflag |= ICANON;
  223.   if (output_only && !(sg_inferior.c_lflag & ICANON))
  224.     sg_ours.c_lflag &= ~ICANON;
  225. #else /* not HAVE_TERMIO */
  226.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  227.   if (output_only)
  228.     sg_ours.sg_flags |= (RAW | CBREAK) & sg_inferior.sg_flags;
  229. #endif /* not HAVE_TERMIO */
  230.  
  231.   fcntl (0, F_SETFL, tflags_ours);
  232.   fcntl (0, F_SETFL, tflags_ours);
  233.   ioctl (0, TIOCSETN, &sg_ours);
  234.  
  235. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  236.   ioctl (0, TIOCSETC, &tc_ours);
  237. #endif
  238. #ifdef TIOCGLTC
  239.   ioctl (0, TIOCSLTC, <c_ours);
  240. #endif
  241. #ifdef TIOCLGET
  242.   ioctl (0, TIOCLSET, &lmode_ours);
  243. #endif
  244.  
  245. #ifdef HAVE_TERMIO
  246.   sg_ours.c_lflag |= ICANON;
  247. #else /* not HAVE_TERMIO */
  248.   sg_ours.sg_flags &= ~RAW & ~CBREAK;
  249. #endif /* not HAVE_TERMIO */
  250. }
  251.  
  252. /* ARGSUSED */
  253. void
  254. term_info (arg, from_tty)
  255.      char *arg;
  256.      int from_tty;
  257. {
  258.   target_terminal_info (arg, from_tty);
  259. }
  260.  
  261. /* ARGSUSED */
  262. void
  263. child_terminal_info (args, from_tty)
  264.      char *args;
  265.      int from_tty;
  266. {
  267.   register int i;
  268.  
  269.   printf_filtered ("Inferior's terminal status (currently saved by GDB):\n");
  270.  
  271. #ifdef HAVE_TERMIO
  272.  
  273.   printf_filtered ("fcntl flags = 0x%x, c_iflag = 0x%x, c_oflag = 0x%x,\n",
  274.       tflags_inferior, sg_inferior.c_iflag, sg_inferior.c_oflag);
  275.   printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
  276.       sg_inferior.c_cflag, sg_inferior.c_lflag, sg_inferior.c_line);
  277.   printf_filtered ("c_cc: ");
  278.   for (i = 0; (i < NCC); i += 1)
  279.     printf_filtered ("0x%x ", sg_inferior.c_cc[i]);
  280.   printf_filtered ("\n");
  281.  
  282. #else /* not HAVE_TERMIO */
  283.  
  284.   printf_filtered ("fcntl flags = 0x%x, sgttyb.sg_flags = 0x%x, owner pid = %d.\n",
  285.       tflags_inferior, sg_inferior.sg_flags, pgrp_inferior);
  286.  
  287. #endif /* not HAVE_TERMIO */
  288.  
  289. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  290.   printf_filtered ("tchars: ");
  291.   for (i = 0; i < (int)sizeof (struct tchars); i++)
  292.     printf_filtered ("0x%x ", ((char *)&tc_inferior)[i]);
  293.   printf_filtered ("\n");
  294. #endif
  295.  
  296. #ifdef TIOCGLTC
  297.   printf_filtered ("ltchars: ");
  298.   for (i = 0; i < (int)sizeof (struct ltchars); i++)
  299.     printf_filtered ("0x%x ", ((char *)<c_inferior)[i]);
  300.   printf_filtered ("\n");
  301. #endif
  302.   
  303. #ifdef TIOCLGET
  304.   printf_filtered ("lmode:  0x%x\n", lmode_inferior);
  305. #endif
  306. }
  307.  
  308. /* NEW_TTY is called in new child processes under Unix, which will
  309.    become debugger target processes.
  310.    If the TTYNAME argument is non-null, we switch to that tty for further
  311.    input and output.  In either case, we remember the setup.  */
  312.  
  313. void
  314. new_tty (ttyname)
  315.      char *ttyname;
  316. {
  317.   register int tty;
  318.  
  319.   /* Save the name for later, for determining whether we and the child
  320.      are sharing a tty.  */
  321.   inferior_thisrun_terminal = ttyname;
  322.   if (ttyname == 0)
  323.     return;
  324.  
  325. #ifdef TIOCNOTTY
  326.   /* Disconnect the child process from our controlling terminal.  */
  327.   tty = open("/dev/tty", O_RDWR);
  328.   if (tty > 0)
  329.     {
  330.       ioctl(tty, TIOCNOTTY, 0);
  331.       close(tty);
  332.     }
  333. #endif
  334.  
  335.   /* Now open the specified new terminal.  */
  336.  
  337.   tty = open(ttyname, O_RDWR);
  338.   if (tty == -1)
  339.     {
  340.       print_sys_errmsg (ttyname, errno);
  341.       _exit(1);
  342.     }
  343.  
  344.   /* Avoid use of dup2; doesn't exist on all systems.  */
  345.   if (tty != 0)
  346.     { close (0); dup (tty); }
  347.   if (tty != 1)
  348.     { close (1); dup (tty); }
  349.   if (tty != 2)
  350.     { close (2); dup (tty); }
  351.   if (tty > 2)
  352.     close(tty);
  353. }
  354.  
  355. /* Kill the inferior process.  Make us have no inferior.  */
  356.  
  357. /* ARGSUSED */
  358. static void
  359. kill_command (arg, from_tty)
  360.      char *arg;
  361.      int from_tty;
  362. {
  363.   if (inferior_pid == 0)
  364.     error ("The program is not being run.");
  365.   if (!query ("Kill the inferior process? "))
  366.     error ("Not confirmed.");
  367.   target_kill (arg, from_tty);
  368.  
  369.   /* Killing off the inferior can leave us with a core file.  If so,
  370.      print the state we are left in.  */
  371.   if (target_has_stack) {
  372.     printf_filtered ("In %s,\n", current_target->to_longname);
  373.     if (selected_frame == NULL)
  374.       fputs_filtered ("No selected stack frame.\n", stdout);
  375.     else
  376.       print_sel_frame (0);
  377.   }
  378. }
  379.  
  380. /* The inferior process has died.  Long live the inferior!  */
  381.  
  382. void
  383. generic_mourn_inferior ()
  384. {
  385.   inferior_pid = 0;
  386.   attach_flag = 0;
  387.   mark_breakpoints_out ();
  388.   registers_changed ();
  389.  
  390. #ifdef CLEAR_DEFERRED_STORES
  391.   /* Delete any pending stores to the inferior... */
  392.   CLEAR_DEFERRED_STORES;
  393. #endif
  394.  
  395.   reopen_exec_file ();
  396.   if (target_has_stack) {
  397.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  398.                       read_pc ()));
  399.     select_frame (get_current_frame (), 0);
  400.   } else {
  401.     set_current_frame (0);
  402.     select_frame ((FRAME) 0, -1);
  403.   }
  404.   /* It is confusing to the user for ignore counts to stick around
  405.      from previous runs of the inferior.  So clear them.  */
  406.   breakpoint_clear_ignore_counts ();
  407. }
  408.  
  409. void
  410. child_mourn_inferior ()
  411. {
  412.   unpush_target (&child_ops);
  413.   generic_mourn_inferior ();
  414. }
  415.  
  416. #if 0 
  417. /* This function is just for testing, and on some systems (Sony NewsOS
  418.    3.2) <sys/user.h> also includes <sys/time.h> which leads to errors
  419.    (since on this system at least sys/time.h is not protected against
  420.    multiple inclusion).  */
  421. /* ARGSUSED */
  422. static void
  423. try_writing_regs_command (arg, from_tty)
  424.      char *arg;
  425.      int from_tty;
  426. {
  427.   register int i;
  428.   register int value;
  429.  
  430.   if (inferior_pid == 0)
  431.     error ("There is no inferior process now.");
  432.  
  433.   /* A Sun 3/50 or 3/60 (at least) running SunOS 4.0.3 will have a
  434.      kernel panic if we try to write past the end of the user area.
  435.      Presumably Sun will fix this bug (it has been reported), but it
  436.      is tacky to crash the system, so at least on SunOS4 we need to
  437.      stop writing when we hit the end of the user area.  */
  438.   for (i = 0; i < sizeof (struct user); i += 2)
  439.     {
  440.       QUIT;
  441.       errno = 0;
  442.       value = call_ptrace (3, inferior_pid, i, 0);
  443.       call_ptrace (6, inferior_pid, i, value);
  444.       if (errno == 0)
  445.     {
  446.       printf (" Succeeded with address 0x%x; value 0x%x (%d).\n",
  447.           i, value, value);
  448.     }
  449.       else if ((i & 0377) == 0)
  450.     printf (" Failed at 0x%x.\n", i);
  451.     }
  452. }
  453. #endif
  454.  
  455. void
  456. _initialize_inflow ()
  457. {
  458.   add_info ("terminal", term_info,
  459.        "Print inferior's saved terminal status.");
  460.  
  461. #if 0
  462.   add_com ("try-writing-regs", class_obscure, try_writing_regs_command,
  463.        "Try writing all locations in inferior's system block.\n\
  464. Report which ones can be written.");
  465. #endif
  466.  
  467.   add_com ("kill", class_run, kill_command,
  468.        "Kill execution of program being debugged.");
  469.  
  470.   inferior_pid = 0;
  471.  
  472.   ioctl (0, TIOCGETP, &sg_ours);
  473.   fcntl (0, F_GETFL, tflags_ours);
  474.  
  475. #if defined(TIOCGETC) && !defined(TIOCGETC_BROKEN)
  476.   ioctl (0, TIOCGETC, &tc_ours);
  477. #endif
  478. #ifdef TIOCGLTC
  479.   ioctl (0, TIOCGLTC, <c_ours);
  480. #endif
  481. #ifdef TIOCLGET
  482.   ioctl (0, TIOCLGET, &lmode_ours);
  483. #endif
  484.  
  485. #ifdef TIOCGPGRP
  486.   ioctl (0, TIOCGPGRP, &pgrp_ours);
  487. #endif /* TIOCGPGRP */
  488.  
  489.   terminal_is_ours = 1;
  490. }
  491.  
  492.